<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-row class="row">
          <el-col :span="18">
            <!-- 表单 -->
            <el-form
              ref="form"
              :inline="true"
              :model="form"
              class="demo-form-inline"
            >
              <el-form-item label="关键字">
                <el-input
                  v-model="form.keyword"
                  placeholder="根据文章标题搜索"
                ></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="form.state" placeholder="请选择状态">
                  <el-option
                    v-for="item in stateArr"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button @click="resetForm">清除</el-button>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6" class="col">
            <el-button type="success" icon="el-icon-edit" @click="addArticle">新增技巧</el-button>
          </el-col>
        </el-row>
        <!-- 消息提醒 -->
        <el-alert
          :title="`数据一共 ${total} 条`"
          type="info"
          show-icon
          :closable="false"
        >
        </el-alert>
        <!-- 表格 -->
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column prop="title" label="文章标题" width="320">
            <template slot-scope="scope">
              <span>{{ scope.row.title }}</span>
              <el-link
                v-if="scope.row.videoURL"
                :underline="false"
                type="primary"
                icon="el-icon-film"
                class="video"
                @click="playVideo(scope.row.videoURL)"
              ></el-link>
            </template>
          </el-table-column>
          <el-table-column prop="visits" label="阅读数"></el-table-column>
          <el-table-column prop="username" label="录入人"></el-table-column>
          <el-table-column prop="createTime" label="录入时间" width="100">
            <template slot-scope="scope">
              {{ scope.row.createTime | formatTime }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="scope">
              {{ scope.row.state === 1 ? "已启用" : "已禁用" }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-link
                type="primary"
                :underline="false"
                @click="previewArticle(scope.row)"
                >预览</el-link
              >
              <el-link type="primary" :underline="false" @click="toggleState(scope.row)">{{
                scope.row.state === 1 ? "禁用" : "启用"
              }}</el-link>
              <el-link
                :type="scope.row.state === 1 ? 'info' : 'primary'"
                :disabled="scope.row.state === 1"
                :underline="false"
                @click="editArticle(scope.row)"
                >修改</el-link
              >
              <el-link
                :type="scope.row.state === 1 ? 'info' : 'primary'"
                :disabled="scope.row.state === 1"
                :underline="false"
                @click="delArticle(scope.row)"
                >删除</el-link
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="form.page"
          :page-sizes="[5, 10, 15, 20, 30]"
          :page-size="form.pagesize"
          layout="prev, pager, next,sizes, jumper"
          :total="total"
        >
        </el-pagination>

        <!-- 预览文章弹框 -->
        <el-dialog
          title="预览文章"
          :visible.sync="showPreviewDialog"
          width="60%"
        >
        <!-- 预览文章组件 -->
        <articles-preview v-if="showPreviewDialog" :article="article"/>
        </el-dialog>

        <!-- 新增文章弹框 -->
        <el-dialog
          :title="isEdit?'编辑文章':'新增文章'"
          :visible.sync="showAddDialog"
          width="60%"
          @close="closeDialog"
        >
        <!-- 添加编辑组件 -->
        <articles-add v-if="showAddDialog" ref="addForm" :isEdit="isEdit" :article="article" @cancel="showAddDialog=false" @update="updateList"/>
        </el-dialog>

        <!-- 视频标签 -->
        <div class="watchVideo" v-if="showVideo">
          <div class="close">
            <span class="el-icon-close" @click="closeVideo"></span>
          </div>
          <div class="video" v-if="showVideo">
            <video :src="videoSrc" autoplay></video>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
// 导入处理时间插件
import dayjs from 'dayjs'
// 导入api
import { list, changeState, remove } from '@/api/hmmm/articles'
// 导入文章预览组件
import ArticlesPreview from '../components/articles-preview.vue'
// 导入添加/编辑文章组件
import ArticlesAdd from '../components/articles-add.vue'
// 导入枚举数据
import { status } from '@/api/hmmm/constants'
export default {
  name: 'Articles',
  components: {
    ArticlesPreview,
    ArticlesAdd
  },
  data () {
    return {
      form: {
        keyword: '', // 关键字
        state: null, // 状态
        page: 1, // 页码
        pagesize: 10 // 每页显示条数
      },
      stateArr: status,
      tableData: [], // 表格数据
      total: 10, // 总条数
      showPreviewDialog: false, // 控制预览文章弹框
      article: null, // 预览/修改文章
      showAddDialog: false, // 添加文章弹框
      isEdit: false, // 是否是编辑
      showVideo: false, // 显示视频
      videoSrc: '' // 视频地址
    }
  },
  created () {
    // 调用获取文章列表方法
    this.getList()
  },
  filters: {
    // 格式化时间
    formatTime (value) {
      return dayjs(value).format('YYYY-MM-DD hh:mm:ss')
    }
  },
  methods: {
    // 点击x按钮关闭遮罩层和视频
    closeVideo () {
      this.showVideo = false
    },

    // 点击视频播放按钮
    playVideo (src) {
      // console.log(src)
      // 视频地址保存在data中
      this.videoSrc = src
      // 打开遮罩层和视频容器
      this.showVideo = true
    },

    // 点击删除按钮
    async delArticle (data) {
      // 显示确认框
      const confirmRes = await this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return
      // 点击确定
      await remove(data)
      // 分页异常
      // 最后一页最后一条数据 分页-1
      if (this.tableData.length === 1) {
        this.form.page--
        // 判断是否是第一页最后一条数据
        if (this.form.page < 1) {
          this.form.page = 1
        }
      }
      // console.log(res)
      // 提示
      this.$message.success('删除成功')
      // 刷新页面
      this.getList()
    },

    // 点击编辑按钮
    editArticle (data) {
      // 向子组件传递当前编辑的文章
      this.article = data
      // 编辑状态
      this.isEdit = true
      // 打开弹框
      this.showAddDialog = true
    },

    // 监听弹框关闭
    // 重置添加文章表单及数据
    closeDialog () {
      // 调用子组件重置表单数据的方法
      this.$refs.addForm.resetForm()
    },

    // 添加成功
    updateList () {
      // 关闭弹框
      this.showAddDialog = false
      // 刷新页面
      this.getList()
    },

    // 点击添加按钮
    addArticle () {
      // 添加状态
      this.isEdit = false
      // 打开弹框
      this.showAddDialog = true
    },

    // 切换状态
    async toggleState (data) {
      // 当前项id
      const id = data.id
      // 判断当前的状态 取反
      if (data.state === 0) {
        await changeState({ id, state: 1 })
        this.$message.success('操作成功')
        // console.log(res)
      } else {
        await changeState({ id, state: 0 })
        this.$message.success('操作成功')
        // console.log(res)
      }
      // 刷新页面
      this.getList()
    },

    // 点击预览按钮
    previewArticle (article) {
      // 向子组件传递当前预览文章
      this.article = article
      // 开启预览文章弹框
      this.showPreviewDialog = true
    },

    // 改变当前每页显示条数
    handleSizeChange (newPagesize) {
      // 当前条数 赋值给表单
      this.form.pagesize = newPagesize
      // 重新发送请求获取数据
      this.getList()
    },

    // 改变当前页
    handleCurrentChange (newPage) {
      // 当前页
      this.form.page = newPage
      // 重新发送请求获取数据
      this.getList()
    },

    // 点击搜索按钮
    async onSubmit () {
      // 回到第一页
      this.form.page = 1
      // 查询 获取文章列表
      this.getList()
      this.$message.success('获取文章列表成功')
    },

    // 点击清除按钮
    resetForm () {
      this.$refs.form.resetFields()
      this.form = {
        keyword: '', // 关键字
        state: null // 状态
      }
      // 清除后 重新获取所有数据
      this.getList()
    },

    // 获取文章列表
    async getList () {
      const res = await list(this.form)
      // console.log(res)
      this.tableData = res.data.items
      this.total = res.data.counts
    }
  }
}
</script>

<style scoped lang="scss">
.col {
  display: flex;
  justify-content: flex-end;
}
.el-alert.el-alert--info.is-light {
  margin-bottom: 20px;
}
.el-pagination {
  text-align: right;
  margin-top: 20px;
}
a.el-link.el-link--primary {
  margin-right: 5px;
}
.el-link.el-link--info.is-disabled {
  color: #c8c9cc;
  margin-right: 5px;
}
.video {
  margin-left: 5px;
  color: blue;
  font-size: 20px;
}

.watchVideo {
  position: fixed;
  top:0;
  left: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.3);
  z-index: 9999;
  .video {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    video {
    width: 100%;
    height: 100%;}
  }
}

span.el-icon-close {
    width: 50px;
    height: 50px;
    font-size: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0,0,0,.4);
    color: #fff;
    border-radius: 50%;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10000;
}
</style>
